Skip to content

refactor: migrate prefer_match_file_name#305

Open
solid-illiaaihistov wants to merge 4 commits into
solid-software:analysis_server_migrationfrom
solid-illiaaihistov:258-migrate-prefer_match_file_name
Open

refactor: migrate prefer_match_file_name#305
solid-illiaaihistov wants to merge 4 commits into
solid-software:analysis_server_migrationfrom
solid-illiaaihistov:258-migrate-prefer_match_file_name

Conversation

@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator

Closes #258

@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@solid-illiaaihistov solid-illiaaihistov linked an issue Jul 1, 2026 that may be closed by this pull request
@solid-illiaaihistov solid-illiaaihistov changed the title 258 migrate prefer match file name refactor: migrate prefer_match_file_name Jul 1, 2026
@solid-illiaaihistov

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.


bool _doNormalizedNamesMatch(String path, String identifierName) {
/// Checks if the normalized file path matches the normalized identifier name.
bool doNormalizedNamesMatch(String path, String identifierName) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we move all of those 3 methods to visitor instead? Feels weird that we have this cross dependency between the two.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@override
void visitClassDeclaration(ClassDeclaration node) {
super.visitClassDeclaration(node);
void visitCompilationUnit(CompilationUnit node) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void visitCompilationUnit(CompilationUnit node) {
    final declarations = node.declarations
        .whereNot(excludedEntities.shouldIgnoreEntity)
        .map(
          (d) {
            final token = switch (d) {
              ClassDeclaration() => d.namePart.typeName,
              ExtensionDeclaration() => d.name,
              MixinDeclaration() => d.name,
              EnumDeclaration() => d.namePart.typeName,
              ExtensionTypeDeclaration() => d.primaryConstructor.typeName,
              _ => null,
            };

            return token == null ? null : (token: token, parent: d);
          },
        )
        .nonNulls
        .multiSortedBy(
          [
            (t) => Identifier.isPrivateName(t.token.lexeme) ? 1 : 0,
            (t) => t.token.offset,
          ],
        );

    if (declarations.isEmpty) return;

    final firstDeclaration = declarations.first;
    final fullName = context.currentUnit?.file.path;

    if (fullName != null &&
        rule.doNormalizedNamesMatch(
          fullName,
          firstDeclaration.token.lexeme,
        )) {
      return;
    }

    final nodeType = humanReadableNodeType(
      firstDeclaration.parent,
    ).toLowerCase();

    final reporter = context.currentUnit?.diagnosticReporter;
    reporter?.atToken(
      firstDeclaration.token,
      rule.diagnosticCode,
      arguments: [nodeType],
    );
  }

To add to utils:

extension<T> on Iterable<T> {
  List<T> multiSortedBy(List<int Function(T)> keys) => sorted(
    (a, b) =>
        keys
            .map((key) => key(a).compareTo(key(b)))
            .firstWhereOrNull((comparison) => comparison != 0) ??
        0,
  );
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

…ity to improve PreferMatchFileName rule encapsulation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate prefer_match_file_name

2 participants